/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alertnetusa.validator;

import com.incident.cap.*;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.StringTokenizer;

/** 
 * An EasCapInfo element for a CAP 1.1 or CAP 1.0 message.  May have one or more EasCapArea and 
 * EasCapResource objects attached. <p>
 * <i>Copyright (c) 2008-2009 Thai Van<p>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * @see EasCapAlert
 * @author Thai Van, Prafash Kumar. This class is based on Art Botterell, David R Robinson work on com.incident.cap.Alert
 * @version 1 Oct 2008
 */
public class EasCapInfo {

    /** String buffer for use through out this class */
    private StringBuffer buf;
    /** the EasCapAlert block of which this EasCapInfo belong to */
    protected EasCapAlert parentAlert = null;
    /** EasCapInfo Language */
    protected String language = null;
    /** ArrayList of EasCapInfo categories */
    protected ArrayList categories = new ArrayList();           //  Event Category
    /** EasCapInfo ListIterator of categories for easy traversal */
    protected ListIterator categoriesListIterator = categories.listIterator();
    /** EasCapInfo event */
    protected String event = null;                              // Event Type
     /** ArrayList of EasCapInfo responseTypes */
    protected ArrayList responseTypes = new ArrayList();        //  Response Types
    /** EasCapInfo ListIterator of responseTypes for easy traversal */
    protected ListIterator responseTypesListIterator = responseTypes.listIterator();
    /** EasCapInfo urgency */
    protected String urgency = null;                            // Urgency
    /** EasCapInfo severity */
    protected String severity = null;                           // Severity
    /** EasCapInfo certainty */
    protected String certainty = null;                          // Certainty
    /** EasCapInfo audience */
    protected String audience = null;                           // Audience
    /** ArrayList of EasCapInfo eventCodes */
    protected ArrayList eventCodes = new ArrayList();           //  Targeting (Event) Codes
    /** EasCapInfo ListIterator of eventCodes for easy traversal */
    protected ListIterator eventCodesListIterator = eventCodes.listIterator();
    /** EasCapInfo effective */
    protected String effective = null;                          // Date/Time Effective
    /** EasCapInfo onset */
    protected String onset = null;                              // Date/Time of Onset
    /** EasCapInfo expires */
    protected String expires = null;                            // Date/Time of Expiration
    /** EasCapInfo senderName */
    protected String senderName = null;                         // Sender's Name
    /** EasCapInfo headline */
    protected String headline = null;                           // Headline
    /** EasCapInfo description */
    protected String description = null;                        // Event Description
    /** EasCapInfo instruction */
    protected String instruction = null;                        // Instructions
    /** EasCapInfo web */
    protected String web = null;                                // Information URL
    /** EasCapInfo contact */
    protected String contact = null;                            // Contact EasCapInfo
    /** ArrayList of EasCapInfo parameters */
    protected ArrayList parameters = new ArrayList();           // Parameters
    /** EasCapInfo ListIterator of parameters for easy traversal */
    protected ListIterator parametersListIterator = parameters.listIterator();
    /** ArrayList of EasCapInfo resources */
    protected ArrayList resources = new ArrayList();            // Resources (ArrayList of EasCapResource objects)
    /** EasCapInfo ListIterator of resources for easy traversal */
    protected ListIterator resourcesListIterator = resources.listIterator();
    /** ArrayList of EasCapInfo areas */
    protected ArrayList areas = new ArrayList();                // Areas (ArrayList of EasCapArea objects)
    /** EasCapInfo ListIterator of areas for easy traversal */
    protected ListIterator areasListIterator = areas.listIterator();

    /** Null constructor */
    public EasCapInfo() {
    }

    /** Optional constructor that sets parent EasCapAlert object
     * @param alert the parent EasCapAlert object
     */
    public EasCapInfo(EasCapAlert alert) {
        setParentAlert(alert);
    }

    /** Get the parent EasCapAlert object */
    public EasCapAlert getParentAlert() {
        return parentAlert;
    }

    /** Set the callback reference to the parent EasCapAlert */
    public void setParentAlert(EasCapAlert alert) {
        parentAlert = alert;
    }

    /** Get language of message  */
    public String getLanguage() {
        return language;
    }

    /** Set language of message  */
    public void setLanguage(String value) {
        language = value;
    }

    /** Append a supplied Event Category to this EasCapInfo  */
    public void addCategory(String value) {
        while (hasNextCategory()) {
            try {
                getNextCategory();
            } catch (Exception e) {
            }
        } // move to end of list
        categoriesListIterator.add(value);
    }

    /** Return number of Event Category in this EasCapInfo  */
    public int countCategories() {
        return categories.size();
    }

    /** Test if there are more items in the iterator */
    public boolean hasNextCategory() {
        return categoriesListIterator.hasNext();
    }

    /** Return the next Event Category from the iterator */
    public String getNextCategory() {
        if (hasNextCategory()) {
            return (String) categoriesListIterator.next();
        } else {
            return null;
        }
    }

    /** Remove Event Category most recently returned by getNextCategory() */
    public void removeCategory() {
        categoriesListIterator.remove();
    }

    /** Reset to index position in Event Category iterator */
    public void resetCategories() {
        categoriesListIterator = categories.listIterator();
    }

    /** Get event type  */
    public String getEvent() {
        return event;
    }

    /** Set event type  */
    public void setEvent(String value) {
        event = value;
    }

    /** Append a supplied Response Type to this EasCapInfo  */
    public void addResponseType(String value) {
        while (hasNextResponseType()) {
            try {
                getNextResponseType();
            } catch (Exception e) {
            }
        } // move to end of list
        responseTypesListIterator.add(value);
    }

    /** Return number of Response Type in this EasCapInfo  */
    public int countResponseTypes() {
        return responseTypes.size();
    }

    /** Test if there are more Response Type items in the iterator */
    public boolean hasNextResponseType() {
        return responseTypesListIterator.hasNext();
    }

    /** Return the next Response Type from the iterator */
    public String getNextResponseType() {
        if (hasNextResponseType()) {
            return (String) responseTypesListIterator.next();
        } else {
            return null;
        }
    }

    /** Remove Response Type most recently returned by getNextCategory() */
    public void removeResponseType() {
        responseTypesListIterator.remove();
    }

    /** Reset to index position in Response Type iterator */
    public void resetResponseTypes() {
        responseTypesListIterator = responseTypes.listIterator();
    }

    /** Get urgency  */
    public String getUrgency() {
        return urgency;
    }

    /** Set urgency  */
    public void setUrgency(String value) {
        urgency = value;
    }

    /** Get severity  */
    public String getSeverity() {
        return severity;
    }

    /** Set severity  */
    public void setSeverity(String value) {
        severity = value;
    }

    /** Get certainty  */
    public String getCertainty() {
        return certainty;
    }

    /** Set certainty  */
    public void setCertainty(String value) {
        if (value.equals("Very Likely")) {
            value = "Likely";
        }
        certainty = value;
    }

    /** Get audience for message  */
    public String getAudience() {
        return audience;
    }

    /** Set audience for message  */
    public void setAudience(String value) {
        audience = value;
    }

    /** Append a supplied Event Code to this EasCapInfo  */
    public void addEventCode(String valueKey, String value) {
        while (hasNextEventCode()) {
            try {
                getNextEventCodeValuePair();
            } catch (Exception e) {
            }
        } // move to end of list
        eventCodesListIterator.add(new ValuePair(valueKey, value));
    }

    /** Append a supplied Event Code in CAP 1.0 format */
    public void addEventCode(String pair) {
        while (hasNextEventCode()) {
            try {
                getNextEventCodeValuePair();
            } catch (Exception e) {
            }
        } // move to end of list
        try {
            StringTokenizer st = new StringTokenizer(pair, "=");
            eventCodesListIterator.add(new ValuePair(st.nextToken(), st.nextToken()));
        } catch (Exception e) {
        }
    }

    /** Return number of Event Code in this EasCapInfo  */
    public int countEventCodes() {
        return eventCodes.size();
    }

    /** Tests if there are more Event Code items in the iterator */
    public boolean hasNextEventCode() {
        return eventCodesListIterator.hasNext();
    }

    /** Return the next Event Code from the iterator as a CAP 1.0-style string */
    public String getNextEventCode() {
        if (hasNextEventCode()) {
            return ((ValuePair) eventCodesListIterator.next()).toCAP10();
        } else {
            return null;
        }
    }

    /** Return the next Event Code as a CAP 1.1-style ValuePair object */
    public ValuePair getNextEventCodeValuePair() {
        if (hasNextEventCode()) {
            return (ValuePair) eventCodesListIterator.next();
        } else {
            return null;
        }
    }

    /** Remove Event Code most recently returned by getNextEventCode() */
    public void removeEventCode() {
        eventCodesListIterator.remove();
    }

    /** Reset to index position in Event Code iterator */
    public void resetEventCodes() {
        eventCodesListIterator = eventCodes.listIterator();
    }

    /** Get effective date/time  */
    public String getEffective() {
        return effective;
    }

    /** Set effective date/time  */
    public void setEffective(String value) {
        effective = value;
    }

    /** Get date/time of event onset  */
    public String getOnset() {
        return onset;
    }

    /** Set date/time of event onset  */
    public void setOnset(String value) {
        onset = value;
    }

    /** Get date/time message expires  */
    public String getExpires() {
        return expires;
    }

    /** Set date/time message expires  */
    public void setExpires(String value) {
        expires = value;
    }

    /** Get sender's human-readable name  */
    public String getSenderName() {
        return senderName;
    }

    /** Set sender's human-readable name  */
    public void setSenderName(String value) {
        senderName = value;
    }

    /** Get message headline  */
    public String getHeadline() {
        return headline;
    }

    /** Set message headline  */
    public void setHeadline(String value) {
        headline = value;
    }

    /** Get event description  */
    public String getDescription() {
        return description;
    }

    /** Set event description  */
    public void setDescription(String value) {
        description = value;
    }

    /** Get instructions for audience  */
    public String getInstruction() {
        return instruction;
    }

    /** Set instructions for audience  */
    public void setInstruction(String value) {
        instruction = value;
    }

    /** Get URL for more information  */
    public String getWeb() {
        return web;
    }

    /** Set URL for more information  */
    public void setWeb(String value) {
        web = value;
    }

    /** Get contact information  */
    public String getContact() {
        return contact;
    }

    /** Set contact information  */
    public void setContact(String value) {
        contact = value;
    }

    /** Append a supplied Parameter to this EasCapInfo  */
    public void addParameter(String valueKey, String value) {
        while (hasNextParameter()) {
            try {
                getNextParameterValuePair();
            } catch (Exception e) {
            }
        } // move to end of list
        parametersListIterator.add(new ValuePair(valueKey, value));
    }

    /** Append a supplied Parameter in CAP 1.0 format */
    public void addParameter(String pair) {
        while (hasNextParameter()) {
            try {
                getNextParameterValuePair();
            } catch (Exception e) {
            }
        } // move to end of list
        try {
            StringTokenizer st = new StringTokenizer(pair, "=");
            parametersListIterator.add(new ValuePair(st.nextToken(), st.nextToken()));
        } catch (Exception e) {
        }
    }

    /** Return number of Parameters in this EasCapInfo  */
    public int countParameters() {
        return parameters.size();
    }

    /** Tests if there are more Parameter items in the iterator */
    public boolean hasNextParameter() {
        return parametersListIterator.hasNext();
    }

    /** Return the next Parameter from the iterator as a CAP 1.0-style string */
    public String getNextParameter() {
        if (hasNextParameter()) {
            return ((ValuePair) parametersListIterator.next()).toCAP10();
        } else {
            return null;
        }
    }

    /** Return the next Parameter as a CAP 1.1-style ValuePair object */
    public ValuePair getNextParameterValuePair() {
        if (hasNextParameter()) {
            return (ValuePair) parametersListIterator.next();
        } else {
            return null;
        }
    }

    /** Remove Parameter most recently returned by getNextParameter() */
    public void removeParameter() {
        parametersListIterator.remove();
    }

    /** Reset to index position in Parameters iterator */
    public void resetParameters() {
        parametersListIterator = parameters.listIterator();
    }

    /** Append a supplied EasCapResource object and return a reference to it  */
    public EasCapResource addResource(EasCapResource res) {
        while (hasNextResource()) {
            try {
                getNextResource();
            } catch (Exception e) {
            }
        } // move to end of list
        resourcesListIterator.add(res);
        return res;
    }

    /** Append an empty EasCapResource object and return a reference to it */
    public EasCapResource addResource() {
        while (hasNextResource()) {
            try {
                getNextResource();
            } catch (Exception e) {
            }
        } // move to end of list
        EasCapResource res = new EasCapResource();
        resourcesListIterator.add(res);
        return res;
    }

    /** Returns number of EasCapResource objects in this EasCapInfo  */
    public int countResources() {
        return resources.size();
    }

    /** Test if there are more EasCapResource items in the iterator */
    public boolean hasNextResource() {
        return resourcesListIterator.hasNext();
    }

    /** Return the next EasCapResource from the iterator */
    public EasCapResource getNextResource() {
        if (hasNextResource()) {
            return (EasCapResource) resourcesListIterator.next();
        } else {
            return null;
        }
    }

    /** Remove EasCapResource most recently returned by getNextResource() */
    public void removeResource() {
        resourcesListIterator.remove();
    }

    /** Reset to index position in EasCapResource iterator */
    public void resetResources() {
        resourcesListIterator = resources.listIterator();
    }

    /** Append a supplied EasCapArea object to this EasCapInfo  */
    public EasCapArea addArea(EasCapArea area) {
        while (hasNextArea()) {
            try {
                getNextArea();
            } catch (Exception e) {
            }
        } // move to end of list
        areasListIterator.add(area);
        return area;
    }

    /** Append an empty EasCapArea object and return a reference to it */
    public EasCapArea addArea() {
        while (hasNextArea()) {
            try {
                getNextArea();
            } catch (Exception e) {
            }
        } // move to end of list
        EasCapArea area = new EasCapArea();
        areasListIterator.add(area);
        return area;
    }

    /** Return number of EasCapArea objects in this EasCapInfo  */
    public int countAreas() {
        return areas.size();
    }

    /** Test if there are more EasCapArea items in the iterator */
    public boolean hasNextArea() {
        return areasListIterator.hasNext();
    }

    /** Return the next EasCapArea from the iterator */
    public EasCapArea getNextArea() {
        if (hasNextArea()) {
            return (EasCapArea) areasListIterator.next();
        } else {
            return null;
        }
    }

    /** Remove EasCapArea most recently returned by getNextArea() */
    public void removeArea() {
        areasListIterator.remove();
    }

    /** Reset to index position in iterator */
    public void resetAreas() {
        areasListIterator = areas.listIterator();
    }

    /** Return the bounding box (latMax, lonMin latMin,lonMax) for this EasCapInfo */
    public String getBounds() {
        String bounds = "";
        double latMax = -90.0;
        double latMin = 90.0;
        double lonMax = -180.0;
        double lonMin = 180.0;
        resetAreas();
        while (hasNextArea()) {
            try {
                String thisBox = getNextArea().getBounds();
                StringTokenizer st = new StringTokenizer(thisBox);
                String vertex = st.nextToken();
                StringTokenizer st2 = new StringTokenizer(vertex, ",");
                double thisLatMax = Double.parseDouble(st2.nextToken());
                double thisLonMin = Double.parseDouble(st2.nextToken());
                vertex = st.nextToken();
                st2 = new StringTokenizer(vertex, ",");
                double thisLatMin = Double.parseDouble(st2.nextToken());
                double thisLonMax = Double.parseDouble(st2.nextToken());
                if (thisLatMax > latMax) {
                    latMax = thisLatMax;
                }
                if (thisLatMin < latMin) {
                    latMin = thisLatMin;
                }
                if (thisLonMax > lonMax) {
                    lonMax = thisLonMax;
                }
                if (thisLonMin < lonMin) {
                    lonMin = thisLonMin;
                }
            } catch (Exception e) {
            }
        }
        bounds = Double.toString(latMax) + "," + Double.toString(lonMin) + " " + Double.toString(latMin) + "," + Double.toString(lonMax);
        return bounds;
    }

    /** Return raw EAS-CAP Info fragment */
    public String toEASCAP() {
        buf = new StringBuffer();

        if (language != null) {
            buf.append("<language>" + EasCapAlert.entities(language) + "</language>\n");
        }

        resetCategories();
        while (hasNextCategory()) {
            buf.append("<category>" + EasCapAlert.entities(getNextCategory()) + "</category>\n");
        }

        if (event != null) {
            buf.append("<event>" + EasCapAlert.entities(event) + "</event>\n");
        } else {
            buf.append("<event>" + EasCapAlert.entities("Undefined event") + "</event>\n");
        }

        resetResponseTypes();
        while (hasNextResponseType()) {
            buf.append("<responseType>" + EasCapAlert.entities(getNextResponseType()) + "</responseType>\n");
        }

        if (urgency != null) {
            buf.append("<urgency>" + EasCapAlert.entities(urgency) + "</urgency>\n");
        } else {
            buf.append("<urgency>" + EasCapAlert.entities("Unknown") + "</urgency>\n");
        }

        if (severity != null) {
            buf.append("<severity>" + EasCapAlert.entities(severity) + "</severity>\n");
        } else {
            buf.append("<severity>" + EasCapAlert.entities("Unknown") + "</severity>\n");
        }

        if (certainty != null) {
            buf.append("<certainty>" + EasCapAlert.entities(certainty) + "</certainty>\n");
        } else {
            buf.append("<certainty>" + EasCapAlert.entities("Unknown") + "</certainty>\n");
        }

        if (audience != null) {
            buf.append("<audience>" + EasCapAlert.entities(audience) + "</audience>\n");
        }

        resetEventCodes();
        while (hasNextEventCode()) {
            buf.append("<eventCode>\n" + EasCapAlert.indent(getNextEventCodeValuePair().toString()) + "\n</eventCode>\n");
        }

        if (effective != null) {
            buf.append("<effective>" + EasCapAlert.entities(effective) + "</effective>\n");
        }

        if (onset != null) {
            buf.append("<onset>" + EasCapAlert.entities(onset) + "</onset>\n");
        }

        if (expires != null) {
            buf.append("<expires>" + EasCapAlert.entities(expires) + "</expires>\n");
        }

        if (senderName != null) {
            buf.append("<senderName>" + EasCapAlert.entities(senderName) + "</senderName>\n");
        }

        if (headline != null) {
            buf.append("<headline>" + EasCapAlert.entities(headline) + "</headline>\n");
        }

        if (description != null) {
            buf.append("<description>" + EasCapAlert.entities(description) + "</description>\n");
        }

        if (instruction != null) {
            buf.append("<instruction>" + EasCapAlert.entities(instruction) + "</instruction>\n");
        }

        if (web != null) {
            buf.append("<web>" + EasCapAlert.entities(web) + "</web>\n");
        }

        if (contact != null) {
            buf.append("<contact>" + EasCapAlert.entities(contact) + "</contact>\n");
        }

        resetParameters();
        while (hasNextParameter()) {
            buf.append("<parameter>\n" + EasCapAlert.indent(getNextParameterValuePair().toString()) + "</parameter>\n");
        }

        resetResources();
        while (hasNextResource()) {
            buf.append("<resource>\n" + EasCapAlert.indent(getNextResource().toEASCAP()) + "</resource>\n");
        }

        resetAreas();
        if (hasNextArea()) {
            buf.append("<area>\n" + EasCapAlert.indent(getNextArea().toEASCAP()) + "</area>\n");
        }

        return buf.toString();
    }

    /** Override toString() to return XML fragment */
    public String toString() {
        buf = new StringBuffer();

        if (language != null) {
            buf.append("<language>" + EasCapAlert.entities(language) + "</language>\n");
        }

        resetCategories();
        while (hasNextCategory()) {
            buf.append("<category>" + EasCapAlert.entities(getNextCategory()) + "</category>\n");
        }

        if (event != null) {
            buf.append("<event>" + EasCapAlert.entities(event) + "</event>\n");
        } else {
            buf.append("<event>" + EasCapAlert.entities("Undefined event") + "</event>\n");
        }

        resetResponseTypes();
        while (hasNextResponseType()) {
            buf.append("<responseType>" + EasCapAlert.entities(getNextResponseType()) + "</responseType>\n");
        }

        if (urgency != null) {
            buf.append("<urgency>" + EasCapAlert.entities(urgency) + "</urgency>\n");
        } else {
            buf.append("<urgency>" + EasCapAlert.entities("Unknown") + "</urgency>\n");
        }

        if (severity != null) {
            buf.append("<severity>" + EasCapAlert.entities(severity) + "</severity>\n");
        } else {
            buf.append("<severity>" + EasCapAlert.entities("Unknown") + "</severity>\n");
        }

        if (certainty != null) {
            buf.append("<certainty>" + EasCapAlert.entities(certainty) + "</certainty>\n");
        } else {
            buf.append("<certainty>" + EasCapAlert.entities("Unknown") + "</certainty>\n");
        }

        if (audience != null) {
            buf.append("<audience>" + EasCapAlert.entities(audience) + "</audience>\n");
        }

        resetEventCodes();
        while (hasNextEventCode()) {
            buf.append("<eventCode>\n" + EasCapAlert.indent(getNextEventCodeValuePair().toString()) + "\n</eventCode>\n");
        }

        if (effective != null) {
            buf.append("<effective>" + EasCapAlert.entities(effective) + "</effective>\n");
        }

        if (onset != null) {
            buf.append("<onset>" + EasCapAlert.entities(onset) + "</onset>\n");
        }

        if (expires != null) {
            buf.append("<expires>" + EasCapAlert.entities(expires) + "</expires>\n");
        }

        if (senderName != null) {
            buf.append("<senderName>" + EasCapAlert.entities(senderName) + "</senderName>\n");
        }

        if (headline != null) {
            buf.append("<headline>" + EasCapAlert.entities(headline) + "</headline>\n");
        }

        if (description != null) {
            buf.append("<description>" + EasCapAlert.entities(description) + "</description>\n");
        }

        if (instruction != null) {
            buf.append("<instruction>" + EasCapAlert.entities(instruction) + "</instruction>\n");
        }

        if (web != null) {
            buf.append("<web>" + EasCapAlert.entities(web) + "</web>\n");
        }

        if (contact != null) {
            buf.append("<contact>" + EasCapAlert.entities(contact) + "</contact>\n");
        }

        resetParameters();
        while (hasNextParameter()) {
            buf.append("<parameter>\n" + EasCapAlert.indent(getNextParameterValuePair().toString()) + "</parameter>\n");
        }

        resetResources();
        while (hasNextResource()) {
            buf.append("<resource>\n" + EasCapAlert.indent(getNextResource().toEASCAP()) + "</resource>\n");
        }

        resetAreas();
        while (hasNextArea()) {
            buf.append("<area>\n" + EasCapAlert.indent(getNextArea().toEASCAP()) + "</area>\n");
        }
        return buf.toString();
    }

    /** return a CAP 1.0 fragment */
    public String toCAP10() {
        // build the EasCapInfo contents in a StringBuffer
        buf = new StringBuffer();
        if (language != null) {
            buf.append("<language>" + EasCapAlert.entities(language) + "</language>\n");
        }
        resetCategories();
        while (hasNextCategory()) {
            buf.append("<category>" + EasCapAlert.entities(getNextCategory()) + "</category>\n");
        }
        buf.append("<event>" + EasCapAlert.entities(event) + "</event>\n");
        resetResponseTypes();
        while (hasNextResponseType()) {
            buf.append("<responseType>" + EasCapAlert.entities(getNextResponseType()) + "</responseType>\n");
        }
        if (urgency != null) {
            buf.append("<urgency>" + EasCapAlert.entities(urgency) + "</urgency>\n");
        } else {
            buf.append("<urgency>" + EasCapAlert.entities("Unknown") + "</urgency>\n");
        }
        if (severity != null) {
            buf.append("<severity>" + EasCapAlert.entities(severity) + "</severity>\n");
        } else {
            buf.append("<severity>" + EasCapAlert.entities("Unknown") + "</severity>\n");
        }
        String certaintyValue = certainty;
        if (certaintyValue.equals("Very Likely")) {
            certaintyValue = "Likely";
        }
        if (certainty != null) {
            buf.append("<certainty>" + EasCapAlert.entities(certaintyValue) + "</certainty>\n");
        } else {
            buf.append("<certainty>" + EasCapAlert.entities("Unknown") + "</certainty>\n");
        }
        if (audience != null) {
            buf.append("<audience>" + EasCapAlert.entities(audience) + "</audience>\n");
        }
        resetEventCodes();
        while (hasNextEventCode()) {
            buf.append("<eventCode>" + EasCapAlert.entities(getNextEventCode()) + "</eventCode>\n");
        }
        if (effective != null) {
            buf.append("<effective>" + EasCapAlert.entities(effective) + "</effective>\n");
        }
        if (onset != null) {
            buf.append("<onset>" + EasCapAlert.entities(onset) + "</onset>\n");
        }
        if (expires != null) {
            buf.append("<expires>" + EasCapAlert.entities(expires) + "</expires>\n");
        }
        if (senderName != null) {
            buf.append("<senderName>" + EasCapAlert.entities(senderName) + "</senderName>\n");
        }
        if (headline != null) {
            buf.append("<headline>" + EasCapAlert.entities(headline) + "</headline>\n");
        }
        if (description != null) {
            buf.append("<description>" + EasCapAlert.entities(description) + "</description>\n");
        }
        if (instruction != null) {
            buf.append("<instruction>" + EasCapAlert.entities(instruction) + "</instruction>\n");
        }
        if (web != null) {
            buf.append("<web>" + EasCapAlert.entities(web) + "</web>\n");
        }
        if (contact != null) {
            buf.append("<contact>" + EasCapAlert.entities(contact) + "</contact>\n");
        }
        resetParameters();
        while (hasNextParameter()) {
            buf.append("<parameter>" + EasCapAlert.entities(getNextParameter()) + "</parameter>\n");
        }
        resetResources();
        while (hasNextResource()) {
            buf.append("<resource>\n" + EasCapAlert.indent(getNextResource().toCAP10()) + "</resource>\n");
        }
        resetAreas();
        while (hasNextArea()) {
            buf.append("<area>\n" + EasCapAlert.indent(getNextArea().toCAP10()) + "</area>\n");
        }
        return buf.toString();

    }
}
